<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>

<body>
  <ul>
    <li>塞尔比</li>
    <li>希金斯</li>
    <li>特鲁姆普</li>
  </ul>
  <script>
    // 函数中this指向有4种

    // 1.纯粹的函数中 指向window 
    var name = "王铁锤"
    function add() {
      console.log(this.name)
    }
    add();

    // 2.对象中的this指向 上层对象
    var car = {
      name: "奔驰",
      run: function () {
        console.log(this.name)//this---car
      },
      room: {
        name: "奢华版",
        clear: function () {
          console.log(this.name)//this---room
        }
      }
    }
    car.run();
    car.room.clear();
    // 3.事件函数中的this指向 当前发生事件的元素
    // box.onclick = function () {
    //    this----box
    // }

    // 4. 构造中的this指向当前实例化对象
    function Person(name, sex) {
      this.name = name;
      this.sex = sex;
      this.eat = function () {
        console.log(this.name)
      }
    }
    var aoshaliwen = new Person("奥沙利文", "男");
    aoshaliwen.eat()

    // 存储this

    var lis = document.querySelectorAll("ul li")
    for (var i = 0; i < lis.length; i++) {
      lis[i].onclick = function () {
        // this  --点击的li
        var that = this
        setTimeout(function () {
          //this--window
          that.style.background = "red"
        }, 3000)
      }
    }


  </script>
</body>

</html>